<?php

namespace App\Middlewares;

use App\Exceptions\BizException;
use App\Exceptions\NeedCompleteUserInfoException;
use App\Exceptions\NotLoginException;
use App\Models\UserModel;
use Base\BaseController;
use Base\MiddlewareInterface;

class NeedComplateUserInfo implements MiddlewareInterface
{
    public static function handle(BaseController &$controller)
    {
        /**
         * 如果不用登录，那就不检查用户信息完备了
         */
        if (!$controller->needLogin) {
            return false;
        }
        /**
         * 如果需要登录 还没有用户信息 那么就让用户去登录
         */
        if ($controller->needLogin && (!$controller->uid || $controller->uid == null)) {
            throw new NotLoginException();
        }
        $user = (new UserModel())->getUserByUid($controller->uid);
        $path = $controller->request()->getUri()->getPath();
        if (in_array($path, [
            'Mp1/Give/sendGive',
            'Mp1/Give/wanna'
        ])) {
            if (empty($user)) {
                throw new NeedCompleteUserInfoException();
            }
            if (empty($user['mobile']) || empty($user['weixin']) || empty($user['nickname'])) {
                throw new NeedCompleteUserInfoException();
            }
        }
    }
}
